import matplotlib.pyplot as plt
%matplotlib inline
from pandas.io.parsers import read_csv
import pickle
import numpy as np
from sklearn.utils import shuffle
import cv2
from tensorflow.contrib.layers import flatten
import tensorflow as tf
import time
import datetime
import os
import random
cd /ibalpowr/workbench/term1-proj2
training_file = 'data/train.p'
validation_file= 'data/valid.p'
testing_file = 'data/test.p'
with open(training_file, mode='rb') as f:
train = pickle.load(f)
with open(validation_file, mode='rb') as f:
valid = pickle.load(f)
with open(testing_file, mode='rb') as f:
test = pickle.load(f)
X_train, y_train = train['features'], train['labels']
X_valid, y_valid = valid['features'], valid['labels']
X_test, y_test = test['features'], test['labels']
X_train.shape
# [num examples, height, width, channels]
# [b,0,1,c]
# height == # of rows
# width == # of cols
# channel in RGB order
n_train = X_train.shape[0]
n_valid = X_valid.shape[0]
n_test = X_test.shape[0]
image_shape = X_train[0].shape
# turn it into a set then count its length
n_classes = len(set(y_train))
print("Number of training examples =", n_train)
print("Number of validation examples =", n_valid)
print("Number of testing examples =", n_test)
print("Image data shape =", image_shape)
print("Number of classes =", n_classes)
labels = read_csv('data/signnames.csv').values[:,1]
plt.hist(y_train, bins=n_classes)
plt.xlim(xmax=n_classes-1)
plt.show
hist, _ = np.histogram(y_train, bins=n_classes)
print(hist)
plt.hist(y_valid, bins=n_classes)
plt.xlim(xmax=n_classes-1)
plt.show
hist_valid, _ = np.histogram(y_valid, bins=n_classes)
print(hist_valid)
unique, index, count = np.unique(y_train, return_index=True,
return_counts=True)
# create a "Contrast Limited Adaptive Histogram Equalization" function
clahe = cv2.createCLAHE(clipLimit=10, tileGridSize=(4,4))
for c, i, k in zip(unique, index, count):
#print("class %i: %s" % (c,labels[c])
print("class " + str(c) + ": " + labels[c])
fig = plt.figure(figsize = (8,2))
# assuming each class been grouped together
# print first five examples of each class
first_five = range(i,i+5,1)
for j in range(5):
# set x and y axes to none
axis = fig.add_subplot(2,10,j+1,xticks=[],yticks=[])
axis.imshow(X_train[first_five[j]])
axis.set_title(first_five[j], fontsize=8)
# print five random examples from each class
random_five = random.sample(range(i, i+k), 5)
for j in range(5):
axis = fig.add_subplot(2,10,j+5+1,xticks=[],yticks=[])
axis.imshow(X_train[random_five[j]])
axis.set_title(random_five[j], fontsize=8)
for j in range(5):
axis = fig.add_subplot(2,10,j+10+1,xticks=[],yticks=[])
axis.imshow(clahe.apply(cv2.cvtColor(X_train[first_five[j]],
cv2.COLOR_RGB2YCrCb)[:,:,0]),
cmap='gray')
axis.set_title('its clahe', fontsize=6)
for j in range(5):
axis = fig.add_subplot(2,10,j+15+1,xticks=[],yticks=[])
axis.imshow(clahe.apply(cv2.cvtColor(X_train[random_five[j]],
cv2.COLOR_RGB2YCrCb)[:,:,0]),
cmap='gray')
axis.set_title('its clahe', fontsize=6)
plt.show()
print('========')
def scale_image(img):
height, width, _ = img.shape
# original image randomly scaled by one number from [0.9, 1.1]
scale_factor = np.random.uniform(0.9, 1.1)
scaled = cv2.resize(img, None, fx=scale_factor, fy=scale_factor,
interpolation = cv2.INTER_AREA)
if scale_factor > 1:
# strip the extra
return scaled[0:32, 0:32]
else:
# repad the border
new_height, new_width, _ = scaled.shape
pad1 = (height - new_height)//2
pad2 = (height - new_height) - pad1
scaled = cv2.copyMakeBorder(scaled, pad1,pad2,pad1,pad2,
borderType=cv2.BORDER_REPLICATE)
return scaled
def translate_image(img):
# randomly shift the original by 2 or less pixels in 4 directions
t = random.sample(range(-2,2),2)
M = np.float32([[1,0,t[0]],[0,1,t[1]]])
rows, cols, _ = img.shape
translated = cv2.warpAffine(img, M, (cols, rows),
borderMode=cv2.BORDER_REPLICATE)
return translated
def rotate_image(img):
# randomly rotate the original by 15 degree cw or ccw
theta = random.sample(range(-15, 15),1)
(height, width) = img.shape[:2]
M = cv2.getRotationMatrix2D((height/2, width/2), theta[0], 1)
rotated = cv2.warpAffine(img, M, (height, width),
borderMode=cv2.BORDER_REPLICATE)
return rotated
f,(ax1,ax2,ax3,ax4) = plt.subplots(1,4,figsize = (10,3))
ax1.imshow(X_train[1000])
ax1.set_title('original', fontsize=10)
ax2.imshow(scale_image(X_train[1000]))
ax2.set_title('scaled', fontsize = 10)
ax3.imshow(translate_image(X_train[1000]))
ax3.set_title('shifted', fontsize=10)
ax4.imshow(rotate_image(X_train[1000]))
ax4.set_title('rotated', fontsize=10)
# for training set
anti_skew_array = np.array([0,6,16,19,
20,21,22,
24,27,29,
30,32,34,
36,37,39,
40,41,42,
14,15,23,26,28,31,33,
3,4,5,7,8,9,10,11,17,18,25,35,1,2,12,13,38])
# 43
# for validation set
anti_skew_array_valid = np.array([0,0,0,0,6,6,6,6,16,16,16,16,19,19,19,19,
20,20,20,20,21,21,21,21,22,22,22,22,
24,24,24,24,27,27,27,27,29,29,29,29,
30,30,30,30,32,32,32,32,34,34,34,34,
36,36,36,36,37,37,37,37,39,39,39,39,
40,40,40,40,41,41,41,41,42,42,42,42,
14,14,15,15,23,23,26,26,28,28,31,31,33,33,
3,4,5,7,8,9,10,11,17,18,25,35,1,2,12,13,38])
# 90 + 17
# to store new images after transformation
new_X_train = []
new_y_train = []
new_hist, _ = np.histogram(y_train, bins=n_classes)
unique, index, count = np.unique(y_train, return_index=True,
return_counts=True)
hist, _ = np.histogram(y_train, bins=n_classes)
while new_hist.sum() < 150000:
# randomly select a class number from anti-skew array
anti_skew_idx = np.random.randint(anti_skew_array.shape[0])
class_no = anti_skew_array[anti_skew_idx]
# randomly select an image
image_idx = np.random.randint(hist[class_no])
X_train_idx = index[class_no] + image_idx
# randomly pick a transformation
# then apply it to the image
# finally append the result to a list
pick = random.sample(range(0,3),1)
if pick[0] == 0:
new_X_train.append(scale_image(X_train[X_train_idx]))
elif pick[0] == 1:
new_X_train.append(translate_image(X_train[X_train_idx]))
else:
new_X_train.append(rotate_image(X_train[X_train_idx]))
new_y_train.append(class_no)
# increment the relevant class size
new_hist[class_no] += 1
new_unique, new_index, new_count = np.unique(new_y_train,
return_index=True,
return_counts=True)
plt.hist(new_y_train, bins=n_classes)
plt.xlim(xmax=n_classes-1)
plt.show
new_hist_after, _ = np.histogram(new_y_train, bins=n_classes)
print(new_hist_after)
new_hist.sum() - hist.sum()
len(new_y_train)
plt.imshow(new_X_train[0])
print('class', new_y_train[0],labels[new_y_train[0]])
new_X_train = np.array(new_X_train)
new_y_train = np.uint8(new_y_train)
X_train_aug = np.concatenate((X_train, new_X_train))
y_train_aug = np.concatenate((y_train, new_y_train))
X_train_aug.shape
plt.hist(y_train_aug, bins=n_classes)
plt.xlim(xmax=n_classes-1)
plt.show
hist_aug, _ = np.histogram(y_train_aug, bins=n_classes)
print(hist_aug)
# to store new images after transformation
new_X_valid = []
new_y_valid = []
new_hist_valid, _ = np.histogram(y_valid, bins=n_classes)
unique_valid, index_valid, count_valid = np.unique(y_valid,
return_index=True,
return_counts=True)
hist_valid, _ = np.histogram(y_valid, bins=n_classes)
while new_hist_valid.sum() < 7000:
# randomly select a class number from anti-skew array
anti_skew_idx = np.random.randint(anti_skew_array_valid.shape[0])
class_no = anti_skew_array_valid[anti_skew_idx]
# randomly select an image
image_idx = np.random.randint(hist_valid[class_no])
X_valid_idx = index_valid[class_no] + image_idx
# randomly pick a transformation
# then apply it to the image
# finally append the result to a list
pick = random.sample(range(0,3),1)
if pick[0] == 0:
new_X_valid.append(scale_image(X_valid[X_valid_idx]))
elif pick[0] == 1:
new_X_valid.append(translate_image(X_valid[X_valid_idx]))
else:
new_X_valid.append(rotate_image(X_valid[X_valid_idx]))
new_y_valid.append(class_no)
# increment the relevant class size
new_hist_valid[class_no] += 1
new_unique_valid,new_index_valid,new_count_valid = np.unique(new_y_valid,
return_index=True,
return_counts=True)
plt.hist(new_y_valid, bins=n_classes)
plt.xlim(xmax=n_classes-1)
plt.show
new_hist_valid_after, _ = np.histogram(new_y_valid, bins=n_classes)
print(new_hist_valid_after)
len(new_y_valid)
new_hist_valid.sum() - hist_valid.sum()
plt.imshow(new_X_valid[0])
print('class', new_y_valid[0],labels[new_y_valid[0]])
new_X_valid = np.array(new_X_valid)
new_y_valid = np.uint8(new_y_valid)
X_valid_aug = np.concatenate((X_valid, new_X_valid))
y_valid_aug = np.concatenate((y_valid, new_y_valid))
X_valid_aug.shape
plt.hist(y_valid_aug, bins=n_classes)
plt.xlim(xmax=n_classes-1)
plt.show
hist_valid_aug, _ = np.histogram(y_valid_aug, bins=n_classes)
print(hist_valid_aug)
X_train_aug_gray =[]
for i in range(len(X_train_aug)):
X_train_aug_gray.append(cv2.cvtColor(X_train_aug[i],
cv2.COLOR_RGB2YCrCb)[:,:,0])
X_train_aug_gray = np.array(X_train_aug_gray)
X_train_aug_gray.shape
X_valid_aug_gray =[]
for i in range(len(X_valid_aug)):
X_valid_aug_gray.append(cv2.cvtColor(X_valid_aug[i],
cv2.COLOR_RGB2YCrCb)[:,:,0])
X_valid_aug_gray = np.array(X_valid_aug_gray)
X_valid_aug_gray.shape
X_test_gray =[]
for i in range(len(X_test)):
X_test_gray.append(cv2.cvtColor(X_test[i],
cv2.COLOR_RGB2YCrCb)[:,:,0])
X_test_gray = np.array(X_test_gray)
clahe = cv2.createCLAHE(clipLimit=10, tileGridSize=(4,4))
X_train_aug_gray_preprocessed = []
for i in range(len(X_train_aug_gray)):
X_train_aug_gray_preprocessed.append(clahe.apply(X_train_aug_gray[i]))
# clipLimit=10, tileGridSize=(4,4)
f, (ax1, ax2, ax3,ax4,ax5) = plt.subplots(1,5,figsize = (10,3))
ax1.imshow(X_train_aug_gray_preprocessed[1000], cmap='gray')
ax1.set_title('clahe', fontsize=10)
ax2.set_xlim(0,256)
ax2.hist(X_train_aug_gray_preprocessed[1000].ravel(),256,[0,256]);
ax2.set_title('clahe histogram', fontsize = 10)
ax3.imshow(X_train_aug[1000])
ax3.set_title('original', fontsize=10)
ax4.imshow(X_train_aug_gray[1000], cmap='gray')
ax4.set_title('channel Y', fontsize=10)
ax5.set_xlim(0,256)
ax5.hist(X_train_aug_gray[1000].ravel(),256,[0,256]);
ax5.set_title('channel Y histogram', fontsize = 10)
X_valid_aug_gray_preprocessed = []
for i in range(len(X_valid_aug_gray)):
X_valid_aug_gray_preprocessed.append(clahe.apply(X_valid_aug_gray[i]))
X_test_gray_preprocessed = []
for i in range(len(X_test_gray)):
X_test_gray_preprocessed.append(clahe.apply(X_test_gray[i]))
X_train_aug_gray_preprocessed = np.array(X_train_aug_gray_preprocessed)
X_train_aug_gray_preprocessed=X_train_aug_gray_preprocessed.reshape(
X_train_aug_gray_preprocessed.shape+(1,))
X_train_aug_gray_preprocessed_shuffled, y_train_aug_shuffled = \
shuffle(X_train_aug_gray_preprocessed,
y_train_aug, random_state = 86)
X_valid_aug_gray_preprocessed = np.array(X_valid_aug_gray_preprocessed)
X_valid_aug_gray_preprocessed = X_valid_aug_gray_preprocessed.reshape(
X_valid_aug_gray_preprocessed.shape + (1,))
X_valid_aug_gray_preprocessed_shuffled, y_valid_aug_shuffled = \
shuffle(X_valid_aug_gray_preprocessed,
y_valid_aug, random_state = 86)
X_test_gray_preprocessed = np.array(X_test_gray_preprocessed)
X_test_gray_preprocessed = X_test_gray_preprocessed.reshape(
X_test_gray_preprocessed.shape + (1,))
X_test_gray_preprocessed_shuffled, y_test_shuffled = \
shuffle(X_test_gray_preprocessed, y_test,
random_state = 86)
def LeNet5(x,keep_prob):
# conv1
mu = 0 # zero mean for weight initialization
sigma = 0.1 # unit variance for weigth initialization
# 6 of (5x5)x1 filters
# output of conv1: 6 feature(activation) maps, each with size 28x28
conv1_W = tf.Variable(tf.truncated_normal(shape=(5, 5, 1, 6), \
mean = mu, stddev = sigma))
# xavier initialization
#conv1_W = tf.get_variable('weights_conv1',shape=(5,5,1,6),
# initializer = tf.contrib.layers.xavier_initializer())
conv1_b = tf.Variable(tf.zeros(6))
# zero padding, 1x1 stride
conv1 = tf.nn.conv2d(x, conv1_W, strides=[1, 1, 1, 1], \
padding='VALID') + conv1_b
# relu1
relu1 = tf.nn.relu(conv1)
# max_pool1
# pool_size is 2x2 and pool_stride is 2x2
# output of max_pool1: 6 feature maps, each with size 14x14
max_pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], \
strides=[1, 2, 2, 1], padding='VALID')
# conv2
# 16 of (5x5)x6 filters
# output of conv2: 16 feature maps, each with size 10x10
conv2_W = tf.Variable(tf.truncated_normal(shape=(5, 5, 6, 16),\
mean = mu, stddev = sigma))
# xavier initialization
#conv2_W = tf.get_variable('weights_conv2',shape=(5,5,6,16),
# initializer = tf.contrib.layers.xavier_initializer())
conv2_b = tf.Variable(tf.zeros(16))
# zero padding, 1x1 stride
conv2 = tf.nn.conv2d(max_pool1, conv2_W, strides=[1, 1, 1, 1], \
padding='VALID') + conv2_b
# relu2
relu2 = tf.nn.relu(conv2)
# max_pool2
# pool_size is 2x2 and pool_stride is 2x2
# output of max_pool2: 16 feature maps, each with size 5x5
max_pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1],\
strides=[1, 2, 2, 1], padding='VALID')
# flat
# output: one vector with length 400
# (5x5)x16 = 400
flat = flatten(max_pool2)
# fc1
# 120 neurons
# output: one vector with length 120
fc1_W = tf.Variable(tf.truncated_normal(shape=(400, 120), \
mean = mu, stddev = sigma))
# xavier initialization
#fc1_W = tf.get_variable('weights_fc1',shape=(400,120),
# initializer = tf.contrib.layers.xavier_initializer())
fc1_b = tf.Variable(tf.zeros(120))
fc1 = tf.matmul(flat, fc1_W) + fc1_b
# relu3
relu3 = tf.nn.relu(fc1)
# dropout1
dropout1 = tf.nn.dropout(relu3, keep_prob)
# fc2
# 84 neurons
# output: one vector with length 84
fc2_W = tf.Variable(tf.truncated_normal(shape=(120, 84), \
mean = mu, stddev = sigma))
# xavier initialization
#fc2_W = tf.get_variable('weights_fc2',shape=(120,84),
# initializer = tf.contrib.layers.xavier_initializer())
fc2_b = tf.Variable(tf.zeros(84))
fc2 = tf.matmul(dropout1, fc2_W) + fc2_b
# relu4
relu4 = tf.nn.relu(fc2)
# dropout2
dropout2 = tf.nn.dropout(relu4, keep_prob)
# fc3
# 43 neurons
# output: one vector with length 43
fc3_W = tf.Variable(tf.truncated_normal(shape=(84, 43), \
mean = mu, stddev = sigma))
# xavier initialization
#fc3_W = tf.get_variable('weights_fc3',shape=(84,43),
# initializer = tf.contrib.layers.xavier_initializer())
fc3_b = tf.Variable(tf.zeros(43))
logits = tf.matmul(dropout2, fc3_W) + fc3_b
# a vector of classification probabilities
return logits
# define input size: image of 32x32 with one channel
# batch size is undefined
x = tf.placeholder(tf.float32, (None, 32, 32, 1))
# define label
y = tf.placeholder(tf.int32, (None))
# convert label to one hot representation
one_hot_y = tf.one_hot(y, 43)
# define a dropout percentage variable
keep_prob = tf.placeholder(tf.float32)
# learning rate
rate = 0.001
# create a LeNet5 instance
logits = LeNet5(x, keep_prob)
# define a loss function
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(\
labels=one_hot_y,logits=logits)
# define an optimization routine
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation)
# batch size depends on the memory size
BATCH_SIZE = 128
correct_prediction = tf.equal(tf.argmax(logits, 1),\
tf.argmax(one_hot_y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, \
tf.float32))
saver = tf.train.Saver()
def evaluate_acc(X_data, y_data):
num_examples = len(X_data)
total_accuracy = 0
sess = tf.get_default_session()
for offset in range(0, num_examples, BATCH_SIZE):
batch_x = X_data[offset:offset+BATCH_SIZE]
batch_y = y_data[offset:offset+BATCH_SIZE]
accuracy = sess.run(accuracy_operation, \
feed_dict={x: batch_x, y: batch_y, \
keep_prob: 1.0})
total_accuracy += (accuracy * len(batch_x))
return total_accuracy / num_examples
# one epoch is one set of all training images
EPOCH_UPPER_LIMIT = 100
EPOCH = 0
today = datetime.date.today().strftime('%b%d')
HHMM = time.strftime('%I%p%M')
starting_time = time.time()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
num_examples = len(X_train)
valid_accuracy_history = []
valid_average_history = []
train_accuracy_history = []
print("Start training...")
print()
for i in range(EPOCH_UPPER_LIMIT):
X_train_aug_gray_preprocessed_shuffled, y_train_aug_shuffled = \
shuffle(X_train_aug_gray_preprocessed_shuffled,y_train_aug_shuffled)
for offset in range(0, num_examples, BATCH_SIZE):
end = offset + BATCH_SIZE
batch_x = X_train_aug_gray_preprocessed_shuffled[offset:end]
batch_y = y_train_aug_shuffled[offset:end]
sess.run(training_operation, \
feed_dict={x: batch_x, y: batch_y, keep_prob:0.7})
validation_accuracy = evaluate_acc(
X_valid_aug_gray_preprocessed_shuffled,
y_valid_aug_shuffled)
valid_accuracy_history.append(validation_accuracy)
training_accuracy = evaluate_acc(
X_train_aug_gray_preprocessed_shuffled,
y_train_aug_shuffled)
train_accuracy_history.append(training_accuracy)
# exit the loop if validation_accuracy > 0.95
if validation_accuracy > 0.95:
EPOCH = i
print('validation accuracy reaches 0.95 at epoch {}'.format(EPOCH))
print('exit the training loop')
print()
break
# print out average of last three at every 5 epochs
if (i+1) % 5 == 0:
valid_average = sum(valid_accuracy_history[(i-2):])/3
valid_average_history.append(valid_average)
train_average = sum(train_accuracy_history[(i-2):])/3
print("over last three epochs {}, {}, {}".format(i-1,i,i+1))
print("validation accuracy avg = {:.3f}".format(valid_average))
print("training accuracy avg = {:.3f}".format(train_average))
print()
if (len(valid_average_history)>0)and(valid_average_history[0]<0.7):
print('initial validation accuracy avg is below 0.7')
print('exit the training loop')
print()
break
# drop learning rate to 1e-4 when
# validation_accuracy > 0.9 AND
# the difference of successive valid_averages < 0.01
if (validation_accuracy > 0.9) and \
((valid_average_history[-1]-valid_average_history[-2])<0.01)and \
(rate != 0.0001):
rate = 0.0001
print('new learning rate 1e-4 starting from epoch {}'.format(i))
print()
saver.save(sess, './proj2-'+today+'-'+HHMM)
print("Model saved")
print("Training Time:",round(time.time()-starting_time,0)," seconds")
plt.plot(range(len(train_accuracy_history)),train_accuracy_history,'b',\
label ='Training Accuracy')
plt.plot(range(len(valid_accuracy_history)),valid_accuracy_history,'g',\
label ='Validation Accuracy')
plt.legend(loc='center right')
plt.xlabel('epoch')
plt.ylabel('accuracies')
with tf.Session() as sess:
loader = tf.train.import_meta_graph('proj2-'+today+'-'+HHMM+'.meta')
loader.restore(sess, tf.train.latest_checkpoint('./'))
test_accuracy = accuracy_operation.eval(feed_dict={
x: X_test_gray_preprocessed_shuffled,
y: y_test_shuffled,keep_prob:1.0})
print("Test Accuracy = {:.3f}".format(test_accuracy))
import glob
import matplotlib.image as mpimg
new_images = []
youtube_images = glob.glob('data/*.png')
for images in youtube_images:
new_images.append(mpimg.imread(images))
# Define the new image labels based on signnames.csv definitions
y_new_images = [33, 12, 8, 18, 1]
fig = plt.figure(figsize = (10,1))
fig.subplots_adjust(left=0,right=1,bottom=0,top=1,\
hspace=0.05,wspace=0.05)
for i in range(len(new_images)):
axis = fig.add_subplot(1,5,i+1, xticks=[], yticks=[])
axis.imshow(new_images[i])
axis.set_title("%s" % labels[y_new_images[i]])
X_new_images_data = np.array(new_images)
X_new_images_data = np.array(X_new_images_data*255, dtype='uint8')
X_new_images_data_gray = []
for i in range(len(X_new_images_data)):
X_new_images_data_gray.append(cv2.cvtColor(X_new_images_data[i],
cv2.COLOR_RGB2YCrCb)[:,:,0])
X_new_images_data_gray_preprocessed = []
for i in range(len(X_new_images_data_gray)):
X_new_images_data_gray_preprocessed.append(
clahe.apply(X_new_images_data_gray[i]))
X_new_images_data_gray_preprocessed = \
np.array(X_new_images_data_gray_preprocessed)
X_new_images_data_gray_preprocessed= \
X_new_images_data_gray_preprocessed.reshape(
X_new_images_data_gray_preprocessed.shape+(1,))
# returns prediction on new test images
def get_prediction(X_data, y_data):
num_examples = len(X_data)
sess = tf.get_default_session()
for offset in range(0, num_examples, BATCH_SIZE):
batch_x = X_data[offset:offset+BATCH_SIZE]
batch_y = y_data[offset:offset+BATCH_SIZE]
correct_pred = sess.run(correct_prediction, \
feed_dict={x: batch_x, \
y: batch_y, \
keep_prob: 1})
return correct_pred
with tf.Session() as sess:
# load saved weights
loader = tf.train.import_meta_graph('proj2-'+today+'-'+HHMM+'.meta')
loader.restore(sess, tf.train.latest_checkpoint('./'))
# run on new test images
test_prediction=get_prediction(X_new_images_data_gray_preprocessed,\
y_new_images)
print("Prediction Result:", test_prediction)
# earlier ... logits = LeNet5(x, keep_prob)
softmax_prediction = tf.nn.softmax(logits)
# return top 5 predictions in each new test images
def get_top_k(X_data, y_data, top_k_number):
num_examples = len(X_data)
sess = tf.get_default_session()
for offset in range(0, num_examples, BATCH_SIZE):
batch_x = X_data[offset:offset+BATCH_SIZE]
batch_y = y_data[offset:offset+BATCH_SIZE]
softmax_pred = sess.run(softmax_prediction, \
feed_dict={x: batch_x, \
y: batch_y, \
keep_prob: 1.0})
top_k_values, top_k_indices = tf.nn.top_k(softmax_pred, \
k=top_k_number, \
sorted=True)
top_k_val = np.array([top_k_values.eval()])
top_k_idx = np.array([top_k_indices.eval()])
return top_k_val, top_k_idx
with tf.Session() as sess:
# load saved weights
loader = tf.train.import_meta_graph('proj2-'+today+'-'+HHMM+'.meta')
loader.restore(sess, tf.train.latest_checkpoint('./'))
test_accuracy = accuracy_operation.eval(feed_dict={
x: X_new_images_data_gray_preprocessed, \
y: y_new_images, keep_prob: 1.0})
# return top five prediction scores in each new test images
top_k_val, top_k_idx = get_top_k(X_new_images_data_gray_preprocessed, \
y_new_images, 5)
print('1st,2nd,3rd,4th,5th predictions')
print()
print(top_k_idx)
# define a function to show the top five predictions
def display_top_five(idx, images, top_k_val, \
top_k_idx, labels, y_new_images):
print ("1st prediction w/ probability of %.4f: %-30s " % \
(top_k_val[0, idx, 0],labels[top_k_idx[0, idx, 0]]))
print ("2nd prediction w/ probability of %.4f: %-30s " % \
(top_k_val[0, idx, 1],labels[top_k_idx[0, idx, 1]]))
print ("3rd prediction w/ probability of %.4f: %-30s " % \
(top_k_val[0, idx, 2],labels[top_k_idx[0, idx, 2]]))
print ("4th prediction w/ probability of %.4f: %-30s " % \
(top_k_val[0, idx, 3],labels[top_k_idx[0, idx, 3]]))
print ("5th prediction w/ probability of %.4f: %-30s " % \
(top_k_val[0, idx, 4],labels[top_k_idx[0, idx, 4]]))
plt.figure(figsize=(2.5,2.5))
plt.imshow(images[idx])
plt.title("true label: %s " % labels[y_new_images[idx]], color='red')
display_top_five(0, new_images, top_k_val, \
top_k_idx, labels, y_new_images)
display_top_five(1, new_images, top_k_val, \
top_k_idx, labels, y_new_images)
display_top_five(2, new_images, top_k_val, \
top_k_idx, labels, y_new_images)
display_top_five(3, new_images, top_k_val, \
top_k_idx, labels, y_new_images)
display_top_five(4, new_images, top_k_val, \
top_k_idx, labels, y_new_images)